﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.IO;

namespace The_LarX
{
    public class LarCustomContentManager : ContentManager
    {
        public Dictionary<string, ContentLoaded> Content;

        public LarCustomContentManager(ServiceContainer Provider)
            : base(Provider)
        {
            Content = new Dictionary<string, ContentLoaded>();
        }

        public override void Unload()
        {
            base.Unload();

            Content.Clear();
        }

        public virtual T Load<T>(string assetName, out bool FirstTime)
        {
            string lower = assetName.ToLower();
            if (Content.ContainsKey(lower))
            {
                FirstTime = false;
                return (T)Content[lower].Content;
            }
            else
            {
                FirstTime = true;
                T t = base.Load<T>(assetName);

                ContentLoaded loaded = new ContentLoaded(t, lower, this);
                Content.Add(lower, loaded);

                return t;
            }
        }

        public override T Load<T>(string assetName)
        {
            bool k;
            return Load<T>(assetName, out k);
        }

        public object LoadLar(string fileName)
        {
            string path = Path.Combine(this.RootDirectory, fileName);
            if (fileName.Contains(".larimg"))
            {
                LarImage image = LarImage.Load(path, LarImageLoadOptions.None);
                return image;
            }
            if (fileName.Contains(".larscene"))
            {
                Serializer.Load(path);
                return Engine.GameScreens[0];
            }
            return null;
        }
    }
}
